package com.houtai.common.util;


import org.apache.commons.lang.RandomStringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;


public class CommonUtil {

    private static SimpleDateFormat sdf_day = new SimpleDateFormat("yyyyMMdd");
    private static SimpleDateFormat sdf_month = new SimpleDateFormat("yyyyMM");
    private static SimpleDateFormat sdf_year = new SimpleDateFormat("yyyy");
    public static boolean checkFileSize(Long len, int size, String unit) {
//        long len = file.length();
        double fileSize = 0;
        if ("B".equals(unit.toUpperCase())) {
            fileSize = (double) len;
        } else if ("K".equals(unit.toUpperCase())) {
            fileSize = (double) len / 1024;
        } else if ("M".equals(unit.toUpperCase())) {
            fileSize = (double) len / 1048576;
        } else if ("G".equals(unit.toUpperCase())) {
            fileSize = (double) len / 1073741824;
        }
        if (fileSize > size) {
            return false;
        }
        return true;
    }
    /**
     * @return String
     * @Purpose 生成token
     * @author lizhun
     * @version 1.0
     */
    public static String getCity(String ip) {
          return IpAddress.getAddresses("ip=" + ip, "utf-8");
    }
    /**
     * @return String
     * @Purpose 生成token
     * @author lizhun
     * @version 1.0
     */
    public static String getToken() {
        UUID token = UUID.randomUUID();
        return "" + token;
    }
    /**
     * @Purpose  获取四位随机数
     * @author   lizhun
     * @param    length
     * @version  1.0
     * @return   String
     */
    public static String randomString(int length) {
        return RandomStringUtils.random(length, false, true);
    }
    /**
     * @return String
     * @Purpose 获取当前天数
     * @author lizhun
     * @version 1.0
     */
    public static String getDay() {
        Date date = new Date();
        return sdf_day.format(date);
    }
    /**
     * @return String
     * @Purpose 获取当前月份
     * @author lizhun
     * @version 1.0
     */
    public static String getYear() {
        Date date = new Date();
        return sdf_year.format(date);
    }
    /**
     * @return String
     * @Purpose 获取当前月份
     * @author lizhun
     * @version 1.0
     */
    public static String getMonth() {
        Date date = new Date();
        return sdf_month.format(date);
    }

}
